'''

Case Type： 数据库系统
Case Name： 数据库正常运行时进程异常退出
Create At:  2020/06/30
Owner    :  @li-xin12345
Descption:  1.开启事务2.事务未提交或回滚，kill -9杀掉该数据库连接进程

history：
created： 2020/06/30  created by @li-xin12345
'''
import os
import unittest
import time
import _thread
import queue
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Common import Common


class KillDbPid(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case104.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.Constant = Constant()
        self.com = Common()

    def test_tran_nocommit(self):
        # 开启事务并执行一个事务，不做提交
        self.log.info('----------------------------开启事务并执行一个事务，不做提交-----------------------------')
        sql_cmd = '''
                 start transaction;
                 drop table if exists test_zl1; 
                 create table test_zl1(a tinyint,b tinyint,c integer,d bigint);
                 '''
        self.log.info(sql_cmd)
        tr_msg = self.sh_primy.executDbSql(sql_cmd)
        self.log.info(tr_msg)
        res1 = tr_msg.find(self.Constant.TABLE_CREATE_SUCCESS) > -1
        self.assertTrue(res1)

        # kill数据库进程
        self.log.info('----------------------------kill数据库进程-----------------------------')
        msg = self.com.kill_pid(self.rootNode, 9)
        self.assertEqual(msg, '')

        time.sleep(20)

        is_started = self.sh_primy.startDbCluster()
        self.assertTrue(is_started)

        # #验证事务是否回滚
        self.log.info('----------------------------验证事务是否回滚-----------------------------')
        sql_cmd = 'select * from test_zl1;'
        self.log.info(sql_cmd)
        roll_msg = self.sh_primy.executDbSql(sql_cmd)
        self.log.info(roll_msg)
        res3 = roll_msg.find(self.Constant.NOT_EXIST) > -1
        self.assertTrue(res3)
        self.log.info(roll_msg)

    def tearDownTestCase(self):
        self.log.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbClusterStatus('status')
            self.log.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()

                self.assertTrue(is_started)
                self.log.info(f'{is_started}')
                self.log.info(f'db_status: {is_started}')
        finally:
            sql_cmd = 'drop table if exists test_zl1;'
            self.log.info(sql_cmd)
            gres_msg = self.sh_primy.executDbSql(sql_cmd)
            self.log.info(gres_msg)
        self.log.info('------------------reliability_dbsys104 执行完成------------------------------')
